<template>
  <div class="list">
    <div class="item" v-for="item in goodlist" :key="item.id">
      <div class="item-l">
        <img class="item-img" :src="require('../assets'+item.src)" />
      </div>
      <div class="item-r">
        <div class="item-title">{{item.title}}</div>
        <div class="item-price">{{item.price|money}}</div>
        <div class="item-opt">
          <button @click="goodshop(item)">加入购物车</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
	import {mapActions,mapState} from 'vuex'
	export default {
		name:"GoodList",
		created() {
			//获取商品列表
			this.$store.dispatch('goods/getList')
			// console.log('商品列表：'+this.$store.state.goods.list)
		},
		computed:{
			// goodlist(){
			// 	return this.$store.state.goods.list
			// }
			//渲染商品列表
			...mapState('goods',{'goodlist':'list'})
		},
		methods:{
			// goodshop(value){
			// 	console.log(value)
			// 	this.$store.dispatch('shopcart/getshop',value)
			// }
			//加入购物车
			...mapActions('shopcart',{'goodshop':'getshop'})
		},
		filters:{
			//价格前缀
			money(value){
				return '￥'+value
			}
		}
	}
</script>

<style>
.item {
  border-bottom: 1px dashed #ccc;
  padding: 10px;
}
.item::after {
  content: "";
  display: block;
  clear: both;
}
.item-l {
  float: left;
  font-size: 0;
}
.item-r {
  float: left;
  padding-left: 20px;
}
.item-img {
  width: 100px;
  height: 100px;
  border: 1px solid #ccc;
}
.item-title {
  font-size: 14px;
  margin-top: 10px;
}
.item-price {
  margin-top: 10px;
  color: #f00;
  font-size: 15px;
}
.item-opt {
  margin-top: 10px;
}
.item-opt button {
  border: 0;
  background: coral;
  color: #fff;
  padding: 4px 5px;
  cursor: pointer;
}
</style>
